package com.execjar;

import cn.hutool.core.util.StrUtil;
import com.execjar.utils.ArithmeticUtils;
import com.execjar.utils.UdpUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhuzi
 * 2025/5/19 下午2:06
 */
@Slf4j(topic = "瑞德6C卡")
public class AppRdRfid {
    public static void main(String[] args) {
        String ip = args[0];
        int port = Integer.parseInt(args[1]);
        for (int f = 0; f < 10; f++) {
            String order6B = "4006EE01000000CB";
            try {
                String info = UdpUtil.postRe16Self(ip, port, order6B, 252);
                log.info("电子车牌返回报文---{}", info);
                // 判断是否读到电子车牌
                if (StrUtil.isNotBlank(info)) {
                    String rfid = "";
                    info = info.replaceAll(" ", "");// 去除返回的空格
                    String labelM = info.substring(6, 8);// 截取报文中展示的标签数
                    String epc = null;
                    if (info.startsWith("F0")) {
                        if ("01".equals(labelM)) {// 标签个数为1个
                            epc = info.substring(10, 34);
                        } else {
                            List<String> list = new ArrayList<String>();
                            int length = info.length();
                            int num = (length - 12) / 26;
                            for (int i = 0; i < num; i++) {
                                String temp = info.substring(8 + i * 26, 32 + i * 26);
                                if (!temp.startsWith("060038651001") && temp.startsWith("06")) {
                                    list.add(info.substring(10 + i * 26, 34 + i * 26));
                                }
                            }
                            if (list.size() == 1) {
                                epc = list.get(0);
                            } else {
                                log.error("RFID异常-RFID读取多张卡");

                            }
                        }
                        log.info("epc=" + epc);
                        //TID处理
                        if (StrUtil.isNotBlank(epc)) {
                            String order_tid_f = "4016EC06";
                            String order_tid_e = "02000600000000";
                            String order_check = ArithmeticUtils.calComplement(ArithmeticUtils.makeChecksum(order_tid_f.toUpperCase() + epc + order_tid_e));
                            String order = order_tid_f + epc + order_tid_e + order_check;
                            info = UdpUtil.postRe16Self(ip, port, order, 30);
                            log.info("电子车牌返回报文--------" + info);
                            // 判断是否读到电子车牌
                            if (StrUtil.isNotBlank(info)) {
                                info = info.replaceAll(" ", "");// 去除返回的空格
                                if (info.startsWith("F0")) {
                                    rfid = info.substring(6, 30);
                                }
                                log.info("读取到的电子车牌:{}", rfid);

                            }
                        }
                    } else {
                        log.error("RFID异常-RFID读取EPC区异常");

                    }
                }
            } catch (Exception e) {
                log.error("error equ_ip:{}", ip, e);
                log.info("电子车牌异常{}", ip, e);

            }
        }
    }
}
